VB Sample Code for modifying selection formulas and group selection formulas

Selection formulas
Private Sub mnuEditRecordSelection_Click()
Dim result%, jobnum%, mainjob%, textHandle&, TextLength%
Dim SelectionText$

result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.", vbOKOnly + vbCritical, "Serious Error"
End
End If

jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Name from label on sample form
ErrorTrap "OpenPrintJob in EditRecordSelection", jobnum%

' Subreport check - if a subreport is currently selected on the main form, jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in EditRecordSelection", mainjob%
End If

' Get Record Selection formula handle and length
result% = PEGetSelectionFormula(jobnum%, textHandle&, TextLength%)
ErrorTrap "GetSelectionFormula in EditRecordSelection", jobnum%
' Get the string of the formula text
SelectionText$ = String$(TextLength%, 0)
result% = crvbHandleToBstr(textHandle&, SelectionText$, TextLength%)
ErrorTrap "HandleToBstr for SelectionText in EditRecordSelection", jobnum%
' Load the formula edit box with data and show it 1 ' Modally
Load FormulaEdit
CenterForm Sample, FormulaEdit

FormulaEdit.Caption = "Editing Record Selection Formula"
FormulaEdit!lblOriginalFormula.Caption = SelectionText$
FormulaEdit!txtEditFormula.Text = SelectionText$
' The formula edit procedure is placed in an infinite loop to allow for repeat editing and testing
Do While True
FormulaEdit.Show 1 ' Modal
' A button has been pressed on the formula edit form - find out which and act accordingly
Select Case FormulaEdit.Tag
Case "Ok"
' Replace the selection formula text in the print job with the edited formula text
SelectionText$ = FormulaEdit!txtEditFormula.Text & Chr$(0)
result% = PESetSelectionFormula(jobnum%, SelectionText$)
ErrorTrap "SetSelectionFormula in EditRecordSelection", jobnum%
Exit Do
Case "Test"
' In order to test the formula, you have to set it first
SelectionText$ = FormulaEdit!txtEditFormula.Text & Chr$(0)
result% = PESetSelectionFormula(jobnum%, SelectionText$)
ErrorTrap "SetSelectionFormula for Test in EditRecordSelection", jobnum%
result% = PECheckSelectionFormula(jobnum%)
If result% = 0 Then
MsgBox "The formula has an error.", vbOKOnly + vbCritical, "Error!"
Else
MsgBox "The formula has no errors.", vbOKOnly + vbInformation, "No Error"
End If
' Now set the formula back to the original - the user can choose to keep the changes by pressing Ok
SelectionText$ = FormulaEdit!lblOriginalFormula.Caption & Chr$(0)
result% = PESetSelectionFormula(jobnum%, SelectionText$)
ErrorTrap "SetSelectionFormula for Untest in EditRecordSelection", jobnum%
Case "Cancel"
MsgBox "Cancel was pressed on the Formula edit form. The formula will not be changed.", vbOKOnly + vbInformation, "Cancel Pressed"
Exit Do
End Select
Loop
' Finished with the formula editting form - unload it
Unload FormulaEdit

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
' Simplified version of the custom-link preview routine (no custom buttons)
result% = PEOutputToWindow(jobnum%, "Formula Demonstration Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0)
ErrorTrap "OutputtoWindow in EditRecordSelection", jobnum%

result% = PEStartPrintJob(jobnum%, True)
ErrorTrap "StartPrintJob in EditRecordSelection", jobnum%

result% = 1
Do While result% <> 0
DoEvents
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop
End If

' Close print job and engine
' Subreport check - if a subreport is currently open, call PreviewReport to offer a chance to
' preview the main report, then close the subreport and main report
If lblSubreportName.Visible Then
PreviewReport mainjob%
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in EditRecordSelection", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If

PECloseEngine

MsgBox "Record Selection Formula Edit Complete!", vbOKOnly, "Operation Succeeded"

End Sub


Group selection formulas
Private Sub mnuEditGroupSelection_Click()
Dim result%, jobnum%, mainjob%, textHandle&, TextLength%
Dim SelectionText$

result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.", vbOKOnly + vbCritical, "Serious Error"
End
End If

jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Name from label on sample form
ErrorTrap "OpenPrintJob in EditGroupSelection", jobnum%

' Subreport check - if a subreport is currently selected on the main form, jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in EditGroupSelection", mainjob%
End If

' Get Group Selection formula handle and length
result% = PEGetGroupSelectionFormula(jobnum%, textHandle&, TextLength%)
ErrorTrap "GetGroupSelectionFormula in GroupSelection", jobnum%
' Get the string of the formula text
SelectionText$ = String$(TextLength%, 0)
result% = crvbHandleToBstr(textHandle&, SelectionText$, TextLength%)
ErrorTrap "HandleToBstr for Group SelectionText in GroupSelection", jobnum%
' Load the formula edit box with data and show it 1 ' Modally
Load FormulaEdit
CenterForm Sample, FormulaEdit
FormulaEdit.Caption = "Editing Group Selection Formula"
FormulaEdit!lblOriginalFormula.Caption = SelectionText$
FormulaEdit!txtEditFormula.Text = SelectionText$
' The formula edit procedure is placed in an infinite loop to allow for repeat editing and testing
Do While True
FormulaEdit.Show 1 ' Modal
' A button has been pressed on the formula edit form - find out which and act accordingly
Select Case FormulaEdit.Tag
Case "Ok"
' Replace the selection formula text in the print job with the edited formula text
SelectionText$ = FormulaEdit!txtEditFormula.Text & Chr$(0)
result% = PESetGroupSelectionFormula(jobnum%, SelectionText$)
ErrorTrap "SetGroupSelectionFormula in GroupSelection", jobnum%
Exit Do
Case "Test"
' In order to test the formula, you have to set it first
SelectionText$ = FormulaEdit!txtEditFormula.Text & Chr$(0)
result% = PESetGroupSelectionFormula(jobnum%, SelectionText$)
ErrorTrap "SetGroupSelectionFormula for Test in GroupSelection", jobnum%
result% = PECheckGroupSelectionFormula(jobnum%)
If result% = 0 Then
MsgBox "The formula has an error.", vbOKOnly + vbCritical, "Error!"
Else
MsgBox "The formula has no errors.", vbOKOnly + vbInformation, "No Errors"
End If
' Now set the formula back to the original - the user can choose to keep the changes by pressing Ok
SelectionText$ = FormulaEdit!lblOriginalFormula.Caption & Chr$(0)
result% = PESetGroupSelectionFormula(jobnum%, SelectionText$)
ErrorTrap "SetGroupSelectionFormula for Untest in GroupSelection", jobnum%
Case "Cancel"
MsgBox "Cancel was pressed on the Formula edit form. The formula will not be changed.", vbOKOnly + vbInformation, "Cancel Pressed"
Exit Do
End Select
Loop
' Finished with the formula editing form - unload it
Unload FormulaEdit

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
' Simplified version of the custom-link preview routine (no custom buttons)
result% = PEOutputToWindow(jobnum%, "Formula Demonstration Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0)
ErrorTrap "OutputtoWindow in GroupSelection", jobnum%

result% = PEStartPrintJob(jobnum%, True)
ErrorTrap "StartPrintJob in GroupSelection", jobnum%

result% = 1
Do While result% <> 0
DoEvents
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop
End If

' Close print job and engine
' Subreport check - if a subreport is currently open, call PreviewReport to offer a chance to
' preview the main report, then close the subreport and main report
If lblSubreportName.Visible Then
PreviewReport mainjob%
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in GroupSelection", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If

PECloseEngine

MsgBox "Group Selection Formula Edit Complete!", vbOKOnly, "Operation Succeeded"

End Sub

ActiveX
Selection formulas and group selection formulas
Private Sub mnuEditSelectionFormulas_Click()
Dim SelForm As String
Dim hwndPreviewWindow As Long

CrystalReport1.ReportFileName = lblReportName.Caption ' Name from label on sample form

' Find out what kind of selection formula to work with - record or group
Select Case MsgBox("Do you want to edit the record selection formula or group selection formula? Press Yes to edit the record selection formula, No to edit the group selection formula, or Cancel:", vbYesNoCancel + vbQuestion, "Which formula to edit?")
Case vbYes
' Get new record selection formula
SelForm = InputBox("Enter additional record selection formula. Press Cancel to abort editing the record selection formula. Field names must be in braces:", "Enter Record Selection Formula", CrystalReport1.SelectionFormula)
' If a zero length string, cancel was pressed - if not, enter new formula
If SelForm <> "" Then
CrystalReport1.SelectionFormula = SelForm
End If
Case vbNo
' Get new group selection formula
SelForm = InputBox("Enter additional group selection formula. Press Cancel to abort editing the group selection formula. Field names must be in braces:", "Enter Group Selection Formula", CrystalReport1.GroupSelectionFormula)
' If a zero length string, cancel was pressed - if not, enter new formula
If SelForm <> "" Then
CrystalReport1.GroupSelectionFormula = SelForm
End If
Case vbCancel
MsgBox "Cancel pressed - no selection formula will be edited.", vbOKOnly + vbCritical, "Cancel Pressed"
End Select

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
CrystalReport1.Destination = 0 ' Window
CrystalReport1.Action = 1 ' Print
ErrorTrap "SelectionFormulas"
hwndPreviewWindow = GetActiveWindow()
Do While IsWindow(hwndPreviewWindow)
DoEvents
Loop
End If

' Close the report
CrystalReport1.ReportFileName = ""

MsgBox "Selection Formula Edit Complete!", vbOKOnly, "Operation Completed"

End Sub


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com